<template>
	<view class="ys">

		<view class="chatlist">
			<scroll-view class="chatscroll" scroll-y :scroll-into-view="scrollView" scroll-with-animation="true"
				@scrolltoupper="toupper" @scrolltolower="tobottom" :refresher-enabled="allow_refresh"
				@refresherpulling="onpull" :refresher-triggered="triggered" @refresherrefresh="onrefresh"
				@refresherrestore="onstore" :refresher-threshold="40" refresher-background="#efefef">
				<view class="chatlength">
					<view :class="item.say_type == 1?'chatitem mychat':'chatitem '" v-for="(item,index) in chatList"
						:key="index" :id="'tab'+item.id">
						<image class="headerimg" src="/static/index/head.png" mode=""></image>
						<view class="item-info">
							<view class="name" v-if="item.say_type==1">
								我
							</view>
							<view class="name" v-else>
								机器人
							</view>
							<view class="item-msg">
								<rich-text :nodes="item.content" @itemclick="itemclick"></rich-text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="sendmsg">
			<view class="top">
				<textarea v-model="content" :style="minHeight" @linechange="changeLine"></textarea>
				<text @click="sendMsg">发送</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		assistant,
		getArea
	} from "@/api/api.js"
	import emoKeyboard from '@/components/emoji/emoKeyboard.vue';
	export default {
		data() {
			return {
				has_authority: false,
				content: '',
				chatList: [],
				temp_list: [],
				img_url: '',
				img_url_show: '',
				scrollView: 'tab',
				user_id: '',
				triggered: false,
				_freshing: false,
				page: 0,
				total_page: 1,
				allow_refresh: true,
				is_init: true,
				last_id: '',
				minHeight: '',
				show_ex: false,
				data: [],
				is_connect: false,
				area: [],
				top: '',
				city: []
			}
		},
		components: {
			emoKeyboard
		},
		onLoad() {
			let res = uni.getWindowInfo()
			this.top = 'padding-top:' + res.statusBarHeight + 'px'
			this.getArea()
		},
		methods: {
			getArea() {
				getArea().then(res => {
					if (res.code == 0) {
						this.area = res.data
					}
				})
			},
			onchange(e) {
				this.city = e.detail.value

			},
			itemclick(e) {
				this.content = e.detail.node.children[0].text
				this.sendMsg()
			},
			changeLine(e) {
				let height = e.detail.height
				if (height < 40) {

					// #ifdef H5
					this.minHeight = 'min-height:auto'
					// #endif
					// #ifndef H5
					this.minHeight = 'min-height:40px'
					// #endif
				} else if (height < 80) {
					this.minHeight = 'min-height:' + height + 'px'
				}
			},
			async getChatList() {
				let that = this
				await getChatList({
					room_id: this.room_id,
					page: this.page
				}).then(res => {

					that._freshing = false;
					this.temp_list = res.data.chat.data
					// this.chatList = [...res.data.chat.data, ...this.chatList]
					this.total_page = res.data.chat.last_page
					this.last_id = this.temp_list[this.temp_list.length - 1].id

				})
			},
			toupper(e) {
				console.log(e);
				setTimeout(() => {
					this.scrollView = 'tab' + this.chatList[0].id
					console.log(this.scrollView);
				}, 50)
			},
			onrefresh(e) {
				let that = this
				if (that._freshing) return;
				that._freshing = true;
				that.page++;
				this.getChatList()
				setTimeout(() => {
					that.triggered = false;
					this.chatList = [...this.temp_list, ...this.chatList]
					if (this.page < this.total_page) {
						this.allow_refresh = true
					} else {
						this.allow_refresh = false
					}
					console.log('结束', that.triggered);
				}, 500)
				setTimeout(() => {
					if (this.is_init) {
						this.scrollView = 'tab' + this.last_id
					}
					console.log(this.scrollView);
				}, 800)
			},
			onstore(e) {
				this.triggered = 'restore'
				console.log('onstore', e);
			},
			more() {
				let more = []
				// let lenght = more.length
				// this.$nextTick(() => {
				// 	this.scrollView = 's' + lenght
				// })
				// console.log(this.scrollView);
				this.chatList = [...more, ...this.chatList]
			},
			getHeight() {
				setTimeout(() => {
					let info = uni.createSelectorQuery().select('.chatlength')
					info.boundingClientRect(function(data) { //data - 各种参数
						console.log(data.height);
						uni.pageScrollTo({
							scrollTop: data.height
						})
					}).exec()
				})
			},
			bigImg(url) {
				let base = getApp().globalData.data.websiteUrl
				let urls = [base + url]
				uni.previewImage({
					urls: urls
				})
			},
			sendSocketMessage(msg) {
				if (socketOpen) {
					uni.sendSocketMessage({
						data: msg
					});
				}
			},
			sendMsg() {
				if (this.content != '') {
					this.chatList.push({
						say_type: 1,
						content: this.content
					})
				}
				assistant({
					title: this.content,
				}).then(res => {
					this.content = ''
					if (res.code == 0) {
						let is_done = res.data.is_done
						let result = res.data.result
						if (is_done) {
							this.chatList.push({
								say_type: 2,
								content: result
							})
						} else {
							let str = "我不知道你说的什么，请选择以下选项:<br>"
							result.forEach(function(it) {
								str +=
									`<a style="border-bottom:1px solid #5555ff;color:#5555ff;">${it}</a><br>`
							})
							this.chatList.push({
								say_type: 2,
								content: str
							})
						}

						setTimeout(() => {
							this.scrollView = 'tab' + res.data.id
						}, 100)
						// this.getHeight()
					} else {
						this.chatList.push({
							say_type: 2,
							content: res.msg
						})
					}
				})
			},
			test(it) {
				console.log(it);
				this.content = it
				this.sendMsg()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #efefef;
	}

	.tab {
		position: fixed;
		height: 44px;
		background: $uni-bg-color;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-weight: bold;
		font-size: 16px;
		z-index: 2024;
		top: 0;
		border: 0 !important;

		view,
		.input-value {
			font-size: 12px !important;
		}

		.input-value-border {
			border: 0;
		}

		.text-color {
			color: #fff;
		}

		.right {
			width: 30%;
		}

		.left {
			width: 30%;
		}

		.title {
			color: #fff;
		}
	}

	.more_text {
		text-align: center;
		margin-bottom: 16px;
		font-size: 12px
	}

	.more {
		text-align: center;
		padding-bottom: 10px;
	}

	.chatscroll {
		height: calc(100vh - 44px);
	}

	.sendmsg {
		position: fixed;
		width: 100%;
		background: #fff;
		min-height: 40px;
		bottom: 0;
		padding: 5px 0;
		box-sizing: border-box;
		line-height: 20px;

		.top {
			align-items: flex-end;
			display: flex;
			box-sizing: border-box;
			width: 100%;
			padding: 0 10px;
		}

		.emoji {
			width: 100%;
			background: #fff;
		}

		textarea {
			background: #efefef;
			flex: 1;
			padding: 8px;
			border-radius: 5px;
			max-height: 72px;
			height: auto;
			box-sizing: border-box;
		}

		.imgbox {
			height: 40px;
			display: flex;
			align-items: center;
		}

		image {
			width: 30px;
			margin-left: 10px;
			transition: .5;
		}

		.emo {
			image {
				margin-left: 0;
				margin-right: 10px;
			}
		}

		text {
			margin-left: 10px;
			background: #0a9413;
			color: #fff;
			height: 37px;
			width: 65px;
			display: flex;
			justify-content: center;
			align-items: center;
			box-sizing: border-box;
			border-radius: 5px;
			transition: .5;
		}
	}

	.chatlist {
		padding-top: 10px;
	}

	.chatitem {
		padding: 0 10px;
		margin-bottom: 15px;

		.headerimg {
			margin-top: 5px;
			width: 35px;
			height: 35px;
			border-radius: 5px;
			float: left;
		}

		.item-info {
			width: calc(90vw - 55px);
			padding-left: 10px;
			float: left;

			.name {
				color: #777;
				font-size: 12px;
				margin-bottom: 3px;

				text {
					margin-left: 5px;
				}
			}

			.item-msg {
				max-width: 100%;
				font-size: 14px;
				float: left;

				text {
					padding: 5px 10px;
					border-radius: 5px;
					background: #fff;
					display: block;
				}
			}

			.item-imgbox {
				image {
					max-width: 30%;
				}
			}
		}
	}

	.chatitem::after {
		content: '';
		display: block;
		height: 0;
		clear: both;
		visibility: hidden;
	}

	.mychat {
		.headerimg {
			float: right;
		}

		.item-info {
			float: right;
			padding-left: 0;
			padding-right: 10px;

			.name {
				text-align: right;
			}

			.item-msg {
				float: right;
			}

			.item-imgbox {
				text-align: right;
			}

		}
	}
</style>